﻿using CPPEI.Coolzon.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace CPPEI.Coolzon.UI.Design
{
    /// <summary>
    /// 添加了一个新组件
    /// </summary>
    /// <param name="parentPart"></param>
    /// <param name="addedPart"></param>
    public delegate void AddPartCompletedEventHandler(UI.IPartContainer parentPart, UI.Part addedPart);

    /// <summary>
    /// 选中一个组件实例
    /// </summary>
    /// <param name="part"></param>
    //public delegate void PartSelectedEventHandler(UI.Part part);

    public partial class DesignData
    {
        public bool IsResize { get; set; }
        public int ResizeDirection { get; set; }
        /// <summary>
        /// 在调整大小位置时鼠标点击的偏移值， 目前只有在移动位置时这个值才有意义
        /// </summary>
        public Point ResizeOffset { get; set; }

        /// <summary>
        /// 场景到画布的偏移值
        /// </summary>
        public Point OffsetToCanvas { get; set; }

        /// <summary>
        /// 场景到屏幕的偏移值
        /// </summary>
        //public Point OffsetToScreen { get; set; }

        /// <summary>
        /// 鼠标按下时的实际坐标，相对于设计的场景来计算
        /// </summary>
        public Point LMouseDownPointInContainer { get; set; }
        /// <summary>
        /// 鼠标弹起时的实际坐标，相对于设计的场景来计算
        /// </summary>
        public Point LMouseUpActualPoint { get; set; }

        /// <summary>
        /// 鼠标按下是的坐标，相对于整个设计画面，用于显示
        /// </summary>
        public Point? LMouseDownPointInCanvas { get; set; }
        public Point LMouseCurrentPoint { get; set; }
        public Point LMouseUpPoint { get; set; }

        public Point RMouseDownPoint { get; set; }

        /// <summary>
        /// 当前选中的组件库项目
        /// </summary>
        public Metadata.PartLibItem SelectedPartLibItem { get; set; }

        public DesignState DesignState { get; private set; }

        private DesignBorder _ctlDesignBorder;
        private RenderPanel _ctlRenderPanel;
        private Canvas _ctlDesignCanvas;
        private UI.Part _selectedPartIntance;

        public DesignData(RenderPanel renderPanel, Canvas designCanvas, DesignBorder designBorder)
        {
            this._ctlDesignBorder = designBorder;
            this._ctlRenderPanel = renderPanel;
            this._ctlDesignCanvas = designCanvas;

            DesignState = UI.DesignState.None;

            this._ctlDesignBorder.SizePointPress += _ctlDesignBorder_SizePointPress;
            this._ctlDesignBorder.SwitchArea += _ctlDesignBorder_SwitchArea;
        }

        public event AddPartCompletedEventHandler AddPartCompleted;
        public event PartSelectedEventHandler PartSelected;

        internal void AddPart(UI.IPartContainer parentPart, UI.Part addedPart)
        {
            if (AddPartCompleted != null)
            {
                AddPartCompleted(parentPart, addedPart);
            }
        }

        internal void SelectPart(UI.Part partInstance)
        {
            Point offsetOfPart = partInstance.TranslatePoint(new Point(0, 0), _ctlRenderPanel);
            //Debug.wr

            _selectedPartIntance = partInstance;
            _ctlDesignBorder.SetTargetPart(_selectedPartIntance);
            _ctlDesignBorder.Visibility = Visibility.Visible;

            Point borderOffset = this.PointElementPartToCanvas(this._selectedPartIntance, new Point(0, 0));
            //Logger.Debug("Designer", "toCanvas-x:{0},y{1} left:{2},top:{3}", OffsetToCanvas.X, OffsetToCanvas.Y, _selectedPartIntance.GetXOffsetLeft(), _selectedPartIntance.GetXOffsetTop());
            //borderOffset.X = offsetOfPart.X;// _selectedPartIntance.GetXOffsetLeft() + OffsetToCanvas.X;
            //borderOffset.Y = offsetOfPart.Y;// _selectedPartIntance.GetXOffsetTop() + OffsetToCanvas.Y;
            _ctlDesignBorder.SetOffset(borderOffset);

            if (PartSelected != null)
            {
                PartSelected(_selectedPartIntance);
            }
        }

        void _ctlDesignBorder_SwitchArea(AreaPart area)
        {
            this._ctlRenderPanel.SwitchToArea("", "");
        }
        
        void _ctlDesignBorder_SizePointPress(int posIndex, Point pointInCanvas,  Point offset)
        {
            IsResize = true;
            ResizeDirection = posIndex;
            ResizeOffset = offset;

            this.ResizeBeginPointInCanvas = pointInCanvas;
            this.ResizeBeginPartPos = new Point(this._selectedPartIntance.XLeft, this._selectedPartIntance.XTop);
            this.ResizeBeginPartSize = new Size(this._selectedPartIntance.XWidth, this._selectedPartIntance.XHeight);
        }

        public void EndResize()
        {
            //_ctlDesignBorder.Visibility = Visibility.Hidden;
            IsResize = false;
        }

        public void UpdateState(DesignState state)
        {
            if (state == UI.DesignState.None)
            {
                _ctlDesignBorder.Visibility = Visibility.Hidden;
            }
            else if (state == UI.DesignState.LayoutPart)
            {
                _ctlDesignBorder.Visibility = Visibility.Hidden;
            }
            else if(state == UI.DesignState.SelectedPart)
            {
                _ctlDesignBorder.Visibility = Visibility.Visible;
            }
            this.DesignState = state;
        }

        private int _partId = 0;
        public string GetPartName()
        {
            _partId++;
            return string.Format("Part{0}", _partId);
        }
    }

    /// <summary>
    /// 设计器的状态
    /// </summary>
    public enum DesignState
    {
        /// <summary>
        /// 默认状态
        /// </summary>
        None,
        /// <summary>
        /// 放置组件
        /// </summary>
        LayoutPart,
        /// <summary>
        /// 选中了一个组件
        /// </summary>
        SelectedPart,
    }
}
